/* -*- c++ -*- */
/*
 * Copyright 2007 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 021-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "lpc_lpc_encoder.h"
#include "lpc/lpc.h"
#include <gr_io_signature.h>
#include <stdexcept>

lpc_lpc_encoder_sptr
lpc_make_lpc_encoder ()
{
  return lpc_lpc_encoder_sptr (new lpc_lpc_encoder ());
}

lpc_lpc_encoder::lpc_lpc_encoder ()
  : gr_sync_decimator ("lpc_lpc_encoder",
		       gr_make_io_signature (1, 1, sizeof (short)),
		       gr_make_io_signature (1, 1, LPC_ENCODED_FRAME_SIZE),
		       LPC_SAMPLES_PER_FRAME)
{
  if ((d_lpc = create_lpc_encoder_state ()) == 0)
    throw std::runtime_error ("lpc_lpc_encoder: create_lpc_encoder_state failed");

  init_lpc_encoder_state(d_lpc);
}

lpc_lpc_encoder::~lpc_lpc_encoder ()
{
  destroy_lpc_encoder_state (d_lpc);
}

int
lpc_lpc_encoder::work (int noutput_items,
			gr_vector_const_void_star &input_items,
			gr_vector_void_star &output_items)
{
  const short *in = (const short *) input_items[0];
  unsigned char *out = (unsigned char *) output_items[0];

  for (int i = 0; i < noutput_items; i++){
    lpc_encode (const_cast<short*>(in), out, d_lpc);
    in += LPC_SAMPLES_PER_FRAME;
    out += LPC_ENCODED_FRAME_SIZE;
  }

  return noutput_items;
}
